package com.sweets.look.ui;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.util.Log;

import com.bilibili.magicasakura.utils.ThemeUtils;
import com.sweet.satellite.Config;
import com.sweet.satellite.GlobalConfig;
import com.sweet.satellite.SatelliteSDK;
import com.sweets.look.R;
import com.sweets.look.adapter.HistoryItem;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.browser.BookmarkManager;
import com.sweets.look.util.ThemeHelper;
import com.sweets.look.util.ViewUtils;
import com.xiaomi.mistatistic.sdk.MiStatInterface;
import com.xiaomi.mistatistic.sdk.URLStatsRecorder;
import com.xiaomi.mistatistic.sdk.controller.HttpEventFilter;
import com.xiaomi.mistatistic.sdk.data.HttpEvent;

public class App extends Application implements ThemeUtils.switchColor{
    private static Context context;
    private static Handler mMainHandler;

    public static Context getAppContext() {
        return context;
    }

    public static Handler getMainHandler() {
        return mMainHandler;
    }

    private String MY_APPID = "2882303761517493192";
    private String MY_APP_KEY = "5541749352192";
    private String CHANNEL = "xiaomi";


    @Override
    public void onCreate() {
        super.onCreate();
        context = this.getApplicationContext();
        mMainHandler = new Handler();

//      CrashUtils.install();

        GlobalConfig.setAppContext(this);
        if (Config.getIsFirstLaunch()) {
            String[] bookmark_urls = context.getResources().getStringArray(R.array.bookmark_url);
            String[] bookmark_titles = context.getResources().getStringArray(R.array.bookmark_title);

            BookmarkManager mBookmarkManager = new BookmarkManager(context);
            for (int i = 0; i < bookmark_urls.length; i++) {
                HistoryItem item = new HistoryItem(bookmark_urls[i], bookmark_titles[i]);
                mBookmarkManager.addBookmark(item);
            }
            PreferenceManager.getInstance().setFullScreenEnabled(true);//自动隐藏搜索栏
        }

        SatelliteSDK.onFirstLaunch();

        initXiaomi();
        initThemes();
    }

    private void initThemes() {
        ThemeUtils.setSwitchColor(this);
        //初始化主题
        ViewUtils.initTheme(this);
    }

    private void initXiaomi() {
        // regular stats.
        MiStatInterface.initialize(this.getApplicationContext(), MY_APPID, MY_APP_KEY,
                CHANNEL);
        MiStatInterface.setUploadPolicy(MiStatInterface.UPLOAD_POLICY_WHILE_INITIALIZE, 0);
        MiStatInterface.enableLog();

        // enable exception catcher.
        MiStatInterface.enableExceptionCatcher(true);

        // enable network monitor
        URLStatsRecorder.enableAutoRecord();
        URLStatsRecorder.setEventFilter(new HttpEventFilter() {

            @Override
            public HttpEvent onEvent(HttpEvent event) {
                // returns null if you want to drop this event.
                // you can modify it here too.
                return event;
            }
        });
        Log.d("MI_STAT", MiStatInterface.getDeviceID(this) + " is the device.");
    }

    @Override
    public int replaceColorById(Context context, @ColorRes int colorId) {
        if (ThemeHelper.isDefaultTheme(context)) {
            return context.getResources().getColor(colorId);
        }
        String theme = getTheme(context);
        if (theme != null) {
            colorId = getThemeColorId(context, colorId, theme);
        }
        return context.getResources().getColor(colorId);
    }

    @Override
    public int replaceColor(Context context, @ColorInt int originColor) {
        if (ThemeHelper.isDefaultTheme(context)) {
            return originColor;
        }
        String theme = getTheme(context);
        int colorId = -1;

        if (theme != null) {
            colorId = getThemeColor(context, originColor, theme);
        }
        return colorId != -1 ? getResources().getColor(colorId) : originColor;
    }

    private
    @ColorRes
    int getThemeColor(Context context, int color, String theme) {
        switch (color) {
            case 0xff78cde0:
                return context.getResources().getIdentifier(theme, "color", getPackageName());
            case 0xff1ebbde:
                return context.getResources().getIdentifier(theme + "_dark", "color", getPackageName());
            case 0x9978cde0:
                return context.getResources().getIdentifier(theme + "_trans", "color", getPackageName());
        }
        return -1;
    }

    private
    @ColorRes
    int getThemeColorId(Context context, int colorId, String theme) {
        switch (colorId) {
            case R.color.theme_color_primary:
                return context.getResources().getIdentifier(theme, "color", getPackageName());
            case R.color.theme_color_primary_dark:
                return context.getResources().getIdentifier(theme + "_dark", "color", getPackageName());
            case R.color.theme_color_primary_trans:
                return context.getResources().getIdentifier(theme + "_trans", "color", getPackageName());
        }
        return colorId;
    }

    private String getTheme(Context context) {
        if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_STORM) {
            return "blue";
        } else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_HOPE) {
            return "purple";
        } else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_WOOD) {
            return "green";
        } else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_LIGHT) {
            return "green_light";
        } else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_THUNDER) {
            return "yellow";
        } else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_SAND) {
            return "orange";
        } else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_FIREY) {
            return "red";
        } else if (ThemeHelper.getTheme(context) == ThemeHelper.CARD_SAKURA){
            return "pink";
        }
        return null;
    }
}

